package com.usefullc.system.service;

import com.usefullc.system.query.BaseQuery;
import com.usefullc.system.dao.DataConfigDao;
import com.usefullc.system.query.DataConfigQuery;
import com.usefullc.system.domain.DataConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Sampson
 * @date: 2022-01-09 09:58
 */
@Service
public class DataConfigService extends AbstractBizService<DataConfig,DataConfigQuery> {

    @Autowired
    private DataConfigDao dataConfigDao;

    @Override
    public List<Predicate> getPredicates(Root<DataConfig> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder, DataConfigQuery q) {
        DataConfigQuery thisQuery = (DataConfigQuery) q;
        List<Predicate> predicates = new ArrayList<>();
        if (thisQuery.getEnable() != null) {
            predicates.add(criteriaBuilder.equal(root.get("enable"), thisQuery.getEnable()));
        }
        if (StringUtils.isNotEmpty(thisQuery.getCode())) {
            predicates.add(criteriaBuilder.equal(root.get("code"), thisQuery.getCode()));
        }
        if (StringUtils.isNotEmpty(thisQuery.getValue())) {
            predicates.add(criteriaBuilder.equal(root.get("value"), thisQuery.getValue()));
        }
        return predicates;

    }

    public DataConfig findByCode(String code){
        DataConfigQuery dataConfigQuery = new DataConfigQuery();
        dataConfigQuery.setEnable(true);
        dataConfigQuery.setCode(code);
        return this.findOne(dataConfigQuery);

    }

    public int updateValueByCode(String code,String value){
        return this.dataConfigDao.updateValueByCode(code,value);
    }


}
